<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商家ID" prop="merchantId">-->
<!--        <el-input-->
<!--          v-model="queryParams.merchantId"-->
<!--          placeholder="请输入商家ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="套餐ID" prop="couponId">-->
<!--        <el-input-->
<!--          v-model="queryParams.couponId"-->
<!--          placeholder="请输入套餐ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="套餐名称" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入套餐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总面值" prop="totalFaceValue">
        <el-input
          v-model="queryParams.totalFaceValue"
          placeholder="请输入总面值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="实付金额" prop="actualAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.actualAmount"-->
<!--          placeholder="请输入实付金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间">
        <el-date-picker
          v-model="daterangePayTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付渠道交易号" prop="transNo" label-width="150">
        <el-input
          v-model="queryParams.transNo"
          placeholder="请输入支付渠道交易号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="支付用户id" prop="uniqueId">-->
<!--        <el-input-->
<!--          v-model="queryParams.uniqueId"-->
<!--          placeholder="请输入支付用户id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发放次数" prop="issueNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.issueNum"-->
<!--          placeholder="请输入发放次数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发放金额" prop="issueAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.issueAmount"-->
<!--          placeholder="请输入发放金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="未发放金额" prop="notIssueAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.notIssueAmount"-->
<!--          placeholder="请输入未发放金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['merchant:couponOrder:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['merchant:couponOrder:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['merchant:couponOrder:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:couponOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table max-height="500" v-loading="loading" :data="couponOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="购买记录ID" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderNo" />
<!--      <el-table-column label="商家ID" align="center" prop="merchantId" />-->
      <el-table-column label="商户名称" align="center" prop="merchantName" />
<!--      <el-table-column label="套餐ID" align="center" prop="couponId" />-->
      <el-table-column label="套餐名称" align="center" prop="couponName" />
      <el-table-column label="总面值" align="center" prop="totalFaceValue" />
      <el-table-column label="实付金额" align="center" prop="actualAmount" />
      <el-table-column label="状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道交易号" align="center" prop="transNo" />
      <el-table-column label="支付用户id" align="center" prop="uniqueId" />
      <el-table-column label="发放次数" align="center" prop="issueNum" />
      <el-table-column label="发放金额" align="center" prop="issueAmount" />
      <el-table-column label="未发放金额" align="center" prop="notIssueAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:couponOrder:edit']"
          >查看</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['merchant:couponOrder:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券购买订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
<!--        <el-form-item label="商家ID" prop="merchantId">-->
<!--          <el-input v-model="form.merchantId" placeholder="请输入商家ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="商户名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
<!--        <el-form-item label="套餐ID" prop="couponId">-->
<!--          <el-input v-model="form.couponId" placeholder="请输入套餐ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="套餐名称" prop="couponName">
          <el-input v-model="form.couponName" placeholder="请输入套餐名称" />
        </el-form-item>
        <el-form-item label="总面值" prop="totalFaceValue">
          <el-input v-model="form.totalFaceValue" placeholder="请输入总面值" />
        </el-form-item>
        <el-form-item label="实付金额" prop="actualAmount">
          <el-input v-model="form.actualAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付渠道交易号" prop="transNo">
          <el-input v-model="form.transNo" placeholder="请输入支付渠道交易号" />
        </el-form-item>
        <el-form-item label="支付用户id" prop="uniqueId">
          <el-input v-model="form.uniqueId" placeholder="请输入支付用户id" />
        </el-form-item>
        <el-form-item label="发放次数" prop="issueNum">
          <el-input v-model="form.issueNum" placeholder="请输入发放次数" />
        </el-form-item>
        <el-form-item label="发放金额" prop="issueAmount">
          <el-input v-model="form.issueAmount" placeholder="请输入发放金额" />
        </el-form-item>
        <el-form-item label="未发放金额" prop="notIssueAmount">
          <el-input v-model="form.notIssueAmount" placeholder="请输入未发放金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCouponOrder, getCouponOrder, delCouponOrder, addCouponOrder, updateCouponOrder } from "@/api/merchant/couponOrder";

export default {
  name: "CouponOrder",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券购买订单表格数据
      couponOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      daterangePayTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        merchantId: null,
        couponId: null,
        couponName: null,
        totalFaceValue: null,
        actualAmount: null,
        orderStatus: null,
        payTime: null,
        transNo: null,
        uniqueId: null,
        issueNum: null,
        issueAmount: null,
        notIssueAmount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        merchantId: [
          { required: true, message: "商家ID不能为空", trigger: "blur" }
        ],
        couponId: [
          { required: true, message: "套餐ID不能为空", trigger: "blur" }
        ],
        totalFaceValue: [
          { required: true, message: "总面值不能为空", trigger: "blur" }
        ],
        actualAmount: [
          { required: true, message: "实付金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询优惠券购买订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePayTime && '' != this.daterangePayTime) {
        this.queryParams.params["beginPayTime"] = this.daterangePayTime[0];
        this.queryParams.params["endPayTime"] = this.daterangePayTime[1];
      }
      listCouponOrder(this.queryParams).then(response => {
        this.couponOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        merchantId: null,
        couponId: null,
        couponName: null,
        totalFaceValue: null,
        actualAmount: null,
        orderStatus: null,
        payTime: null,
        transNo: null,
        uniqueId: null,
        issueNum: null,
        issueAmount: null,
        notIssueAmount: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePayTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券购买订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCouponOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券购买订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCouponOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCouponOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除优惠券购买订单编号为"' + ids + '"的数据项？').then(function() {
        return delCouponOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('merchant/couponOrder/export', {
        ...this.queryParams
      }, `couponOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
